package com.itlong.cloud.sdk.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertyEquipGetDTO;
import com.itlong.cloud.POJO.DTO.sdk.SDKEquipGetByPageDTO;
import com.itlong.cloud.POJO.DTO.sdk.SDKEquipGetPageDTO;
import com.itlong.cloud.POJO.DTO.sdk.SDKUnitInfoSaveDTO;
import com.itlong.cloud.POJO.PO.DeviceElevatorAuthPO;
import com.itlong.cloud.POJO.PO.DeviceElevatorBridgeInfoPO;
import com.itlong.cloud.POJO.PO.DeviceElevatorInfoPO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipRecordGetVO;
import com.itlong.cloud.property.service.IPropertyDeviceAuthService;
import com.itlong.cloud.sdk.dao.ISDKServerOldElevatorDao;
import com.itlong.cloud.sdk.dao.ISDKServerOldEntranceDao;
import com.itlong.cloud.sdk.service.ISDKServerOldEquipService;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      SDK设备服务层实现类
 * </desc>
 *
 * @createDate 2017/11/14
 */
@Service
public class SDKServerOldEquipServiceImpl implements ISDKServerOldEquipService {

    @Autowired
    private ISDKServerOldEntranceDao iSDKServerOldEntranceDao;

    @Autowired
    private ISDKServerOldElevatorDao iSDKServerOldElevatorDao;

    @Autowired
    IPropertyDeviceAuthService ipropertyDeviceAuthService;



    /**
     * <desc>
     *      根据地址，分页查询设备信息
     * <desc/>
     * @param       sdkEquipGetPageDTO  待查询的数据封装对象
     * @return      分页设备信息
     * @author Biao.R
     * @createDate 2017/11/16
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyEquipGetDTO> getEquipPageByAddr(SDKEquipGetPageDTO sdkEquipGetPageDTO) {
        String buildId = sdkEquipGetPageDTO.getBuildId();
        String unitId = sdkEquipGetPageDTO.getUnitId();
        Page<PropertyEquipGetDTO> page = new Page<>();
        Map<String, Object> params = new HashMap<>();
        Integer pageSize = sdkEquipGetPageDTO.getPageSize();
        Integer pageNo = sdkEquipGetPageDTO.getPageNo();
        page.getPage().setCurrentPage(pageNo == null ? "1" : pageNo.toString());
        page.getPage().setPageSize(pageSize == null ? "10" : pageSize.toString());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        params.put("projectId", sdkEquipGetPageDTO.getProjectId());
        if (StringUtils.isNotEmpty(buildId)) {
            params.put("buildIdIn", buildId);
            if (StringUtils.isNotEmpty(unitId)) {
                params.put("unitIdIn", unitId);
            }
        }
        page.setRows(iSDKServerOldElevatorDao.getPage(params), iSDKServerOldElevatorDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      根据设备唯一码查询设备归属地编号信息
     * <desc/>
     * @param       deviceUnique     设备唯一码
     * @return      设备归属地址信息  设备归属地编号信息集
     * @author Biao.R
     * @createDate 2017/11/16
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<SDKUnitInfoSaveDTO> getAddrNumByDeviceUnique(String deviceUnique) {
        if(StringUtils.isNotEmpty(deviceUnique)){
        return iSDKServerOldElevatorDao.getAddrNumByDeviceUnique(deviceUnique);
        }
        return null;
    }

    /**
     * <desc>
     *      根据设备唯一码查询设备归属地址信息
     * <desc/>
     * @param      param    查询参数集
     * @return      设备归属地信息集
     * @author Biao.R
     * @createDate 2017/11/16
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<DeviceElevatorBridgeInfoPO> getAddrByParams(Map<String, Object> param) {
        if(param !=null &&!param.isEmpty()){
            return iSDKServerOldElevatorDao.getAddrByParams(param);
        }
        return null;
    }

    /**
     * <desc>
     *      分页查询设备通行记录
     * <desc/>
     * @param       sdkEquipGetByPageDTO    查询参数封装DTO
     * @return      查询出的分页数据
     * @author Biao.R
     * @createDate 2017/11/21
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyEquipRecordGetVO> getEquipRecordPage(SDKEquipGetByPageDTO sdkEquipGetByPageDTO) {

        String deviceUnique = sdkEquipGetByPageDTO.getDeviceUnique();
        String projectId = sdkEquipGetByPageDTO.getProjectId();
        String userPhone = sdkEquipGetByPageDTO.getUserPhone();
        String openType = sdkEquipGetByPageDTO.getOpenType();
        String beginDate = sdkEquipGetByPageDTO.getBeginDate();
        String endDate = sdkEquipGetByPageDTO.getEndDate();
        Map<String,Object> params = new HashMap<>();
        params.put("projectId",projectId);
        if(StringUtils.isNotEmpty(deviceUnique)){
            params.put("deviceUnique",deviceUnique);
        }
        if(StringUtils.isNotEmpty(userPhone)){
            params.put("phoneNum",userPhone);
        }
        if(StringUtils.isNotEmpty(openType)){
            params.put("openType",openType);
        }
        if (StringUtils.isNotEmpty(beginDate)) {
            beginDate += " :00";
            params.put("startTime", beginDate);
        }
        if (StringUtils.isNotEmpty(endDate)) {
            endDate += " :00";
            params.put("endTime", endDate);
        }

        Page<PropertyEquipRecordGetVO> page = new Page<>();
        Integer pageSize = sdkEquipGetByPageDTO.getPageSize();
        Integer pageNo = sdkEquipGetByPageDTO.getPageNo();
        //如果pageSize为null 默认10  pageNo为null,默认1
        page.getPage().setPageSize(pageSize == null ? "10" : pageSize.toString());
        page.getPage().setCurrentPage(pageNo == null ? "1" : pageNo.toString());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        page.setRows(iSDKServerOldElevatorDao.getEquipRecordPage(params), iSDKServerOldElevatorDao.getEquipRecordPageCount(params));
        return page;
    }


    /**
     * <desc>
     *      根据设备唯一码查询设备
     * <desc/>
     * @param   deviceUnique    待查询的设备唯一码
     * @return  查询到的设备
     * @author Biao.R
     * @createDate 2017/11/23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public DeviceElevatorInfoPO getEquipByDeviceUnique(String deviceUnique) {
        return iSDKServerOldElevatorDao.getByDeviceUnique(deviceUnique);
    }

    /**
     * <desc>
     *      更新动态密码到电梯权限表
     * <desc/>
     * @param  elevatorAuthPO 待更新的数据
     * @return  更新的数量
     * @author Biao.R
     * @createDate 2017/11/23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer updateDynamicPwd(DeviceElevatorAuthPO elevatorAuthPO) {
        return iSDKServerOldElevatorDao.updateDynamicPwd(elevatorAuthPO);
    }

    /**
     * <desc>
     *      根据参数查询设备（电梯或门禁）
     * <desc/>
     * @param   params  查询参数
     * @return  查询设备结果集
     * @author Biao.R
     * @createDate 2017/11/25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<PropertyEquipGetDTO> getEquipByParam(Map<String, Object> params) {
        return iSDKServerOldElevatorDao.getEquipByParam(params);
    }

    /**
     * <desc>
     *      根据参数查询最大的deviceId
     * <desc/>
     * @param   params  查询参数
     * @return  查询出的最大deviceId,若没有，则为0
     * @author Biao.R
     * @createDate 2017/11/25
     */
    @Override
    public Integer getMaxDeviceId(Map<String, Object> params) {
        return iSDKServerOldElevatorDao.getMaxDeviceId(params);
    }

    /**
     * <desc>
     *     根据地址查询电梯信息
     * <desc/>
     * @param  projectId    操作的项目编号
     * @param  buildNum     楼栋编号（多个以英文，分隔）
     * @param  unitNum      单元编号（多个以英文，分隔）
     * @return 电梯信息集合
     * @author Biao.R
     * @createDate 2018/3/1
     */
    @Override
    public List<PropertyEquipGetDTO> getElevatorByAddress(String projectId, String buildNum, String unitNum) {
        if(StringUtils.isEmpty(projectId)){
            return null;
        }
        return iSDKServerOldElevatorDao.getElevatorByAddress(projectId,buildNum,unitNum);
    }
}